package com.mnt.washio.activities;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.mnt.washio.R;
import com.mnt.washio.objects.Product;
import com.mnt.washio.utils.Statics;
import com.mnt.washio.utils.network.WebServiceCommunicatorListener;
import com.mnt.washio.utils.network.ws.BaseWSControl;
import com.mnt.washio.utils.network.ws.BaseWSControl.WebServiceFlag;
import com.mnt.washio.utils.network.ws.GetPricingListWSControl;
import com.mnt.washio.views.CustomFontTextView;

public class PricingActivity extends BaseActivity implements
		WebServiceCommunicatorListener {

	private ArrayList<Product> mLaundryPros, mDryCleaningPros;
	private LinearLayout mGroupDryer, mGroupCleaning;
	private LayoutInflater mInflater;
	private ProgressDialog pd;
	private View mViewNoData;
	public String deliverFeeMaximum, deliveryFee, orderMinimum;
	private TextView mTvFreeDelevery, mTvDeleveryFee, mTvOrderMinimum;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pricings);

		initView();
		initProperties();
	}

	@Override
	protected void initView() {
		super.initView();

		mViewNoData = findViewById(R.id.view_nodata);

		mGroupDryer = (LinearLayout) findViewById(R.id.groupDryer);
		mGroupCleaning = (LinearLayout) findViewById(R.id.groupCleaning);

		mTvFreeDelevery = (TextView) findViewById(R.id.tv_free_delivery);
		mTvDeleveryFee = (TextView) findViewById(R.id.tv_delivery_fee);
		mTvOrderMinimum = (TextView) findViewById(R.id.tv_order_minimum);

		mLaundryPros = new ArrayList<Product>();
		mDryCleaningPros = new ArrayList<Product>();

		mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

		pd = new ProgressDialog(this);
		pd.setCancelable(false);
		pd.setMessage("Getting pricing...");
	}

	private void initProperties() {
		new GetPricingListWSControl(this, this, getIntent().getStringExtra(
				"ZIPCODE")).execute();
	}

	private void addCleaningProducts() {
		for (Product p : mLaundryPros) {
			mGroupDryer.addView(getView(p));
			mGroupDryer.addView(getPaddingView());
		}
	}

	private void addDyerProducts() {
		for (Product p : mDryCleaningPros) {
			mGroupCleaning.addView(getView(p));
			mGroupCleaning.addView(getPaddingView());
		}
	}

	private View getView(Product p) {
		View v = mInflater.inflate(R.layout.item_list_product, null);
		CustomFontTextView tvName = (CustomFontTextView) v
				.findViewById(R.id.tv_name);
		tvName.setText(p.getTitle());

		CustomFontTextView tvPrice = (CustomFontTextView) v
				.findViewById(R.id.tv_price);
		try {
			tvPrice.setText(String.format("$%.2f",
					Float.parseFloat(p.getPrice())));
		} catch (Exception e) {
			tvPrice.setText("$" + p.getPrice());
		}

		LayoutParams layoutParams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, Statics.PixelFromDp(this, 50),
				Gravity.CENTER_VERTICAL);
		v.setLayoutParams(layoutParams);

		return v;
	}

	private View getPaddingView() {
		View v = mInflater.inflate(R.layout.layout_padding, null);
		v.setMinimumHeight(Statics.PixelFromDp(this, 2));
		v.setBackgroundColor(Color.TRANSPARENT);
		return v;
	}

	@Override
	public void onConnectionOpen(BaseWSControl wsControl, WebServiceFlag flag) {
		// TODO Auto-generated method stub
		if (flag == WebServiceFlag.GET_PRICING) {
			mViewNoData.setVisibility(View.VISIBLE);
			pd.show();
		}
	}

	@Override
	public void onConnectionError(BaseWSControl wsControl, WebServiceFlag flag,
			String error) {
		// TODO Auto-generated method stub
		if (flag == WebServiceFlag.GET_PRICING) {
			pd.dismiss();
			Statics.showToast(this, error);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onConnectionDone(BaseWSControl wsControl, WebServiceFlag flag,
			Object result) {
		if (flag == WebServiceFlag.GET_PRICING) {
			mViewNoData.setVisibility(View.GONE);
			pd.dismiss();

			mTvFreeDelevery.setText(getString(R.string.txt_pricing_title1,
					doubleFormat(deliverFeeMaximum)));
			mTvDeleveryFee
					.setText(getString(R.string.txt_pricing_title2,
							doubleFormat(deliveryFee),
							doubleFormat(deliverFeeMaximum)));
			mTvOrderMinimum.setText(getString(R.string.txt_pricing_title3,
					doubleFormat(orderMinimum)));

			mTvFreeDelevery.setVisibility(View.VISIBLE);
			mTvDeleveryFee.setVisibility(View.VISIBLE);
			mTvOrderMinimum.setVisibility(View.VISIBLE);

			getAllProduct((List<Product>) result);
		}
	}

	private String doubleFormat(String d) {
		DecimalFormat formater = new DecimalFormat("0.00");
		return formater.format(Double.parseDouble(d));
	}

	@Override
	public void onUpdateData(BaseWSControl wsControl, WebServiceFlag flag,
			Object data) {
		// TODO Auto-generated method stub

	}

	private void getAllProduct(List<Product> result) {
		for (Product product : result) {
			if (product.getCategory().equals(Product.DRYCLEANING)) {
				mDryCleaningPros.add(product);
			} else {
				mLaundryPros.add(product);
			}
		}

		addCleaningProducts();
		addDyerProducts();
	}
}
